package com.ruoyi.web.controller.teaching;

import java.util.Date;
import java.util.List;

import com.ruoyi.teaching.domain.TeachingClasses;
import com.ruoyi.teaching.service.ITeachingSchoolService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.teaching.domain.TeachingStudentApply;
import com.ruoyi.teaching.service.ITeachingStudentApplyService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 学生报课Controller
 * 
 * @author ruoyi
 * @date 2022-10-17
 */
@Controller
@RequestMapping("/teaching/apply")
public class TeachingStudentApplyController extends BaseController
{
    private String prefix = "teaching/apply";

    @Autowired
    private ITeachingStudentApplyService teachingStudentApplyService;

    @Autowired
    private ITeachingSchoolService teachingSchoolService;

    @RequiresPermissions("teaching:apply:view")
    @GetMapping()
    public String apply()
    {
        return prefix + "/apply";
    }

    /**
     * 查询学生报课列表
     */
    @RequiresPermissions("teaching:apply:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TeachingStudentApply teachingStudentApply)
    {
//        logger.info(teachingStudentApply.toString());
        startPage();
//        teachingStudentApply.setSchoolId(getSysUser().getSchoolId());
        List<TeachingStudentApply> list = teachingStudentApplyService.selectTeachingStudentApplyList(teachingStudentApply);
//        for (TeachingStudentApply studentApply :list){
//            Long schoolId = studentApply.getSchoolId();
//            studentApply.setTeachingSchool(teachingSchoolService.selectTeachingSchoolBySchoolId(schoolId));
//        }
        return getDataTable(list);
    }


    /**
     * 导出学生报课列表
     */
    @RequiresPermissions("teaching:apply:export")
    @Log(title = "学生报课", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TeachingStudentApply teachingStudentApply)
    {
        List<TeachingStudentApply> list = teachingStudentApplyService.selectTeachingStudentApplyList(teachingStudentApply);
        ExcelUtil<TeachingStudentApply> util = new ExcelUtil<TeachingStudentApply>(TeachingStudentApply.class);
        return util.exportExcel(list, "学生报课数据");
    }

    /**
     * 新增学生报课
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存学生报课
     */
    @RequiresPermissions("teaching:apply:add")
    @Log(title = "学生报课", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TeachingStudentApply teachingStudentApply)
    {
        return toAjax(teachingStudentApplyService.insertTeachingStudentApply(teachingStudentApply));
    }

    /**
     * 修改学生报课
     */
    @RequiresPermissions("teaching:apply:edit")
    @GetMapping("/edit/{studentId}")
    public String edit(@PathVariable("studentId") Long studentId, ModelMap mmap)
    {
        TeachingStudentApply teachingStudentApply = teachingStudentApplyService.selectTeachingStudentApplyByStudentId(studentId);
        mmap.put("teachingStudentApply", teachingStudentApply);
        return prefix + "/edit";
    }

    /**
     * 修改保存学生报课
     */
    @RequiresPermissions("teaching:apply:edit")
    @Log(title = "学生报课", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TeachingStudentApply teachingStudentApply)
    {
        return toAjax(teachingStudentApplyService.updateTeachingStudentApply(teachingStudentApply));
    }

    /**
     * 修改保存学生报课 LIST
     */
    @PostMapping("/editList")
    @ResponseBody
    public AjaxResult editList(@RequestBody List<TeachingStudentApply> teachingStudentApplys)
    {
        for (TeachingStudentApply teachingStudentApply : teachingStudentApplys)
        {
            teachingStudentApply.setUpdateBy(getLoginName());
            teachingStudentApply.setUpdateTime(new Date());
            teachingStudentApplyService.updateTeachingStudentApply(teachingStudentApply);
        }
        return success();
    }


    /**
     * 删除学生报课
     */
    @RequiresPermissions("teaching:apply:remove")
    @Log(title = "学生报课", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(teachingStudentApplyService.deleteTeachingStudentApplyByStudentIds(ids));
    }

    @PostMapping("/endCourse")
    @ResponseBody
    public AjaxResult endCourse(Long applyId){
        String status = "1";
        TeachingStudentApply studentApply = teachingStudentApplyService.selectTeachingStudentApplyByApplyId(applyId);
        studentApply.setStatus(status);
        studentApply.setUpdateBy(getLoginName());
        studentApply.setUpdateTime(new Date());
        return toAjax(teachingStudentApplyService.updateTeachingStudentApply(studentApply));
    }
}
